<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/25
 * Time: 10:26
 */

class Wecharpay
{
    public $orderId;
    private $info;
    private $hostname;
    private $database;
    private $username;
    private $password;
    public function __construct($db,$orderId)
    {
        $this->hostname =  $db['default']['hostname'];
        $this->database =  $db['default']['database'];
        $this->username =  $db['default']['username'];
        $this->password =  $db['default']['password'];
        $this->orderId = $orderId;
    }

    private function mysqlConnect($type)
    {
        $mysqli = new mysqli($this->hostname, $this->username, $this->password, $this->database);//连接到数据库
        //检查连接是否成功
        if (mysqli_connect_errno()){
            //注意mysqli_connect_error()新特性
            die('Unable to connect!'). mysqli_connect_error();
        }
        if (!$mysqli->set_charset("utf8")) {
            die("Error loading character set utf8"). $mysqli->error;
        }
        $sql = $this->getSql($type);
        $result = $mysqli->query($sql);
        if($result->num_rows>0){
            $this->info = $result->fetch_object();//获取第一行
        }else{
            echo '<meta charset="utf-8"><script type="text/javascript">alert("支付失败");location.href="http://wap.zxkj918.com"</script>';exit;
        }
        $result->free(); //释放资源
        $mysqli->close();
    }

    /**
     * @param $type 1,线路 2,商品合并支付 3，商品单订单支付
     * @return string
     */
    private function getSql($type)
    {
        $sql = '';
        switch($type){
            case 1:
                $sql = 'SELECT sales_order.order_sn,sales_order.order_pay_total,line_order_product.line_name FROM sales_order INNER JOIN
                line_order_product ON line_order_product.order_id = sales_order.order_id
                WHERE sales_order.order_id='.$this->orderId;//执行查询
            break;
            case 2:
                $sql = 'SELECT order_sn,order_amount FROM `tourism_order_main` WHERE order_sn='.$this->orderId.' LIMIT 1';
                break;
            case 3:
                $sql = 'SELECT order_sn,actual_price  FROM `tourism_order` WHERE order_sn='.$this->orderId.' LIMIT 1';
                break;
            default:
                exit('Error !');
                break;
        }
        return $sql;
    }

    public function getData($type)
    {
        $data = array();
        switch($type){
            case 1:
                $data = $this->lineData();
                break;
            case 2:
                $data = $this->tourismData();
                break;
            case 3:
                $data = $this->tourismDataUser();
                break;
            default:
                exit('Error !');
                break;
        }
        return $data;
    }

    private function lineData()
    {
        $data['body'] = $this->info->line_name;
        $data['total_fee'] = bcmul($this->info->order_pay_total, 100, 2);
        $data['notify_url'] = site_url('line/paycallback/wxNotify');
        $data['url'] = $this->config->wap_base_url.'line/payment/wxCompletePay/'.$this->orderId;
        return $data;
    }

    private function tourismData()
    {
        $sign = $this->encryptionMd5($this->info->order_sn);
        $data['body'] = $this->info->order_sn;
        $data['total_fee'] = bcmul($this->info->order_amount, 100, 2);
        $data['notify_url'] = $this->config->wap_base_url.'tourism/paycallback/wxNotify';
        $data['url'] = $this->config->wap_base_url.'tourism/payment/wxCompletePay/'.$this->info->order_sn.'?sign='.$sign;
        return $data;
    }

    private function tourismDataUser()
    {
        $sign = $this->encryptionMd5($this->info->order_sn);
        $data['body'] = '着想测试';
        $data['total_fee'] = $this->info->actual_price;
        $data['notify_url'] = $this->config->wap_base_url.'tourism/paycallback/wxNotifyU';
        $data['url'] = $this->config->wap_base_url.'tourism/usertopay/wxCompletePay/'.$this->info->order_sn.'?sign='.$sign;
        return $data;
    }

    private function encryptionMd5($orderSn)
    {
        return md5($orderSn+'md5');
    }

    public function getOrderInfoToPay($type){
        $this->mysqlConnect($type);
        $data = $this->getData($type);
        return $data;
    }
}